package com.qfedu.servlet;

import com.qfedu.entity.Result;
import com.qfedu.entity.User;
import com.qfedu.service.IUserService;
import com.qfedu.service.impl.UserService;
import com.qfedu.utils.ImageCodeUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    private IUserService userService = new UserService();
    public String addUser(HttpServletRequest request, HttpServletResponse response){

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        System.out.println(email);
        String gender = request.getParameter("gender");
        Result result = userService.addUser(username, password, email, gender);
        //request.getSession().setAttribute("addUserResult",result);
            if(result.isSuccess()){
                //response.sendRedirect("registerSuccess.jsp");
                return "redirect:/registerSuccess.jsp";
            }else{
                request.setAttribute("register.jsp",result);
                //response.sendRedirect("registerSuccess.jsp");
                return "forward:/register.jsp";
            }
        //response.sendRedirect("");
    }
    public Result checkUserByName(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        System.out.println(username);
        Result result = userService.checkUserByName(username);
        request.getSession().setAttribute("checkUserNameResult",result);
        return result;
    }
    public Result checkUserByEmail(HttpServletRequest request, HttpServletResponse response){
        String email = request.getParameter("email");
        //System.out.println(username);
        Result result = userService.checkUserByEmail(email);
        request.getSession().setAttribute("checkUserEmailResult",result);
        return result;
    }
    public Result updateUserFlag(HttpServletRequest request, HttpServletResponse response){
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        System.out.println("bab"+email);
        System.out.println("bab"+code);
        //System.out.println(username);
        Result result = userService.updateUserFlag(code, email);
        request.getSession().setAttribute("updateUserFlagResult",result);
        return result;
    }
    public void checkUser(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String auto = request.getParameter("auto");
        String codeNum = ImageCodeUtil.getCodeNum();
        Result result = userService.checkUser(username, password, code,codeNum);
        User user = (User) result.getDate();
        Cookie cookie = new Cookie("autoLogin",user.getUsername()+":"+user.getPassword());
        //System.out.println(cookie);
        try {
            if (result.isSuccess()){
                if (auto!=null){
                    request.getSession().setAttribute("user",result.getDate());
                    cookie.setMaxAge(3600*24*14);
                    request.getSession().setAttribute("loginCookie",cookie);
                    response.addCookie(cookie);
                    response.sendRedirect("index.jsp");
                }else {
                    request.getSession().setAttribute("user",result.getDate());
                    response.sendRedirect("index.jsp");
                }
            }else {
                request.getSession().setAttribute("loginResult",result);
                //System.out.println(result.getDate());
                response.sendRedirect("login.jsp");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void logOut(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("user");
        try {
            response.sendRedirect("index.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
